﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Hr.DAL
{
    /// <summary>
    /// 数据访问类Pblish_Info。
    /// </summary>
    public class Pblish_Info
    {
        public Pblish_Info()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Pblish_Info where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Hr.Entity.Pblish_Info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Pblish_Info(");
            strSql.Append("RecordId,PublisherId,PublisherName,PublisherCode,PublishTime,Subject,Content,InfoType,isPopUp,DepartId,DepartName,CompanyId,CompanyName,CityId,CityName,Addtime,IsDel)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@PublisherId,@PublisherName,@PublisherCode,@PublishTime,@Subject,@Content,@InfoType,@isPopUp,@DepartId,@DepartName,@CompanyId,@CompanyName,@CityId,@CityName,@Addtime,@IsDel)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "PublisherId", DbType.Guid, model.PublisherId);
            db.AddInParameter(dbCommand, "PublisherName", DbType.AnsiString, model.PublisherName);
            db.AddInParameter(dbCommand, "PublisherCode", DbType.AnsiString, model.PublisherCode);
            db.AddInParameter(dbCommand, "PublishTime", DbType.DateTime, model.PublishTime);
            db.AddInParameter(dbCommand, "Subject", DbType.AnsiString, model.Subject);
            db.AddInParameter(dbCommand, "Content", DbType.AnsiString, model.Content);
            db.AddInParameter(dbCommand, "InfoType", DbType.AnsiString, model.InfoType);
            db.AddInParameter(dbCommand, "isPopUp", DbType.Boolean, model.isPopUp);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "DepartName", DbType.AnsiString, model.DepartName);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "Addtime", DbType.DateTime, model.Addtime);
            db.AddInParameter(dbCommand, "IsDel", DbType.Boolean, model.IsDel);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Hr.Entity.Pblish_Info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Pblish_Info set ");
            strSql.Append("PublisherId=@PublisherId,");
            strSql.Append("PublisherName=@PublisherName,");
            strSql.Append("PublisherCode=@PublisherCode,");
            strSql.Append("PublishTime=@PublishTime,");
            strSql.Append("Subject=@Subject,");
            strSql.Append("Content=@Content,");
            strSql.Append("InfoType=@InfoType,");
            strSql.Append("isPopUp=@isPopUp,");
            strSql.Append("DepartId=@DepartId,");
            strSql.Append("DepartName=@DepartName,");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("CompanyName=@CompanyName,");
            strSql.Append("CityId=@CityId,");
            strSql.Append("CityName=@CityName,");
            strSql.Append("Addtime=@Addtime,");
            strSql.Append("IsDel=@IsDel");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "PublisherId", DbType.Guid, model.PublisherId);
            db.AddInParameter(dbCommand, "PublisherName", DbType.AnsiString, model.PublisherName);
            db.AddInParameter(dbCommand, "PublisherCode", DbType.AnsiString, model.PublisherCode);
            db.AddInParameter(dbCommand, "PublishTime", DbType.DateTime, model.PublishTime);
            db.AddInParameter(dbCommand, "Subject", DbType.AnsiString, model.Subject);
            db.AddInParameter(dbCommand, "Content", DbType.AnsiString, model.Content);
            db.AddInParameter(dbCommand, "InfoType", DbType.AnsiString, model.InfoType);
            db.AddInParameter(dbCommand, "isPopUp", DbType.Boolean, model.isPopUp);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "DepartName", DbType.AnsiString, model.DepartName);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "Addtime", DbType.DateTime, model.Addtime);
            db.AddInParameter(dbCommand, "IsDel", DbType.Boolean, model.IsDel);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Pblish_Info ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Pblish_Info GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,PublisherId,PublisherName,PublisherCode,PublishTime,Subject,Content,InfoType,isPopUp,DepartId,DepartName,CompanyId,CompanyName,CityId,CityName,Addtime,IsDel from Pblish_Info ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Hr.Entity.Pblish_Info model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,PublisherId,PublisherName,PublisherCode,PublishTime,Subject,Content,InfoType,isPopUp,DepartId,DepartName,CompanyId,CompanyName,CityId,CityName,Addtime,IsDel ");
            strSql.Append(" FROM Pblish_Info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strTop, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top " + strTop + " * ");
            strSql.Append(" FROM Pblish_Info a");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageSize, int PageIndex, string OrderField, string TableName, string strWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, strWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "Pblish_Info");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Hr.Entity.Pblish_Info> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,PublisherId,PublisherName,PublisherCode,PublishTime,Subject,Content,InfoType,isPopUp,DepartId,DepartName,CompanyId,CompanyName,CityId,CityName,Addtime,IsDel ");
            strSql.Append(" FROM Pblish_Info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Hr.Entity.Pblish_Info> list = new List<Chenxin.Lib.Hr.Entity.Pblish_Info>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Pblish_Info ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Hr.Entity.Pblish_Info model = new Chenxin.Lib.Hr.Entity.Pblish_Info();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["PublisherId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PublisherId = new Guid(ojb.ToString());
            }
            model.PublisherName = dataReader["PublisherName"].ToString();
            model.PublisherCode = dataReader["PublisherCode"].ToString();
            ojb = dataReader["PublishTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PublishTime = (DateTime)ojb;
            }
            model.Subject = dataReader["Subject"].ToString();
            model.Content = dataReader["Content"].ToString();
            model.InfoType = dataReader["InfoType"].ToString();
            ojb = dataReader["isPopUp"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.isPopUp = (bool)ojb;
            }
            ojb = dataReader["DepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartId = new Guid(ojb.ToString());
            }
            model.DepartName = dataReader["DepartName"].ToString();
            ojb = dataReader["CompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId = new Guid(ojb.ToString());
            }
            model.CompanyName = dataReader["CompanyName"].ToString();
            ojb = dataReader["CityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId = new Guid(ojb.ToString());
            }
            model.CityName = dataReader["CityName"].ToString();
            ojb = dataReader["Addtime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Addtime = (DateTime)ojb;
            }
            ojb = dataReader["IsDel"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsDel = (bool)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}

